package net.fdukedom.epicurus.presentation.actions;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

/**
 * Main class for all forms. Override doReset and doValidate methods to add
 * new functionallity.
 *
 * @author Dmitry Y. Kochelaev
 */
public abstract class AbstractForm extends ActionForm {

    /**
     * Logger for the class.
     */
    protected Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        return doValidate(mapping, request);
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        try {
            request.setCharacterEncoding("windows-1251");
            doReset(mapping, request);
        } catch (UnsupportedEncodingException e) {
            logger.throwing(this.getClass().getName(), "reset", e);
        }
    }

    /**
     * Really validates form.
     *
     * @param mapping action mappings
     * @param request user request
     *
     * @return validation errors
     */
    public abstract ActionErrors doValidate(ActionMapping mapping, HttpServletRequest request);

    /**
     * Really resets form.
     *
     * @param mapping action mappings
     * @param request user request
     */
    public abstract void doReset(ActionMapping mapping, HttpServletRequest request);
}
